package com.ranjiaqi.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName TestHashMap
 * @Description TODO
 * @Author hp
 * @Date 2021/6/25 14:03
 * @Version 1.0
 **/
public class TestHashMap {
    public static void main(String[] args) {
//        stringStudentMap();
            studentStringMap();
    }


    /**
     * HashMap存储数据
     * 键是字符串，值是Student对象
     */
    public static void stringStudentMap() {
        Map<String, Person> map = new HashMap<>();
        map.put("北京市", new Person("冉佳奇", 23));
        map.put("天津市", new Person("郭德纲", 55));
        map.put("上海市", new Person("成龙", 75));

        for (String s : map.keySet()) {
            System.out.println(s + "=" + map.get(s));
        }

        for (Map.Entry<String, Person> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }


    public static void studentStringMap() {
        Map<Person, String> map = new HashMap<>();
        map.put(new Person("冉佳奇", 23), "北京市");
        map.put(new Person("郭德纲", 55), "天津市");
        map.put(new Person("成龙", 75), "上海市");
        map.put(new Person("成龙", 75), "上海市");
        Set<Map.Entry<Person, String>> entrySet = map.entrySet();
        Iterator<Map.Entry<Person, String>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<Person, String> next = iterator.next();
            System.out.println(next.getKey() + "=" + next.getValue());
        }


        System.out.println("========================================");
        Set<Person> set = map.keySet();
        Iterator<Person> iterator1 = set.iterator();
        while (iterator1.hasNext()){
            Person next = iterator1.next();
            System.out.println(next+"= "+map.get(next));
        }
    }
}
